﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using System.Collections.Generic;

namespace G.Data
{
    public class GridListBoxDataSource
    {
        public ObservableCollection<object> DataSource { get; private set; }
        internal Controls.GridListBox GridListBox;

        public GridListBoxDataSource()
        {
            this.DataSource = new ObservableCollection<object>();
        }

        public void Clear()
        {
            this.DataSource.Clear();
            if (GridListBox != null)
                GridListBox.ItemsSource = null;
        }
       
        public void AddRange(object[] data)
        {
            foreach (var item in data)
                DataSource.Add(item);

            ObservableCollection<object[]> itemsSouce = GridListBox.ItemsSource as ObservableCollection<object[]>;
            if (itemsSouce == null)
            {
                itemsSouce = new ObservableCollection<object[]>();
                GridListBox.ItemsSource = itemsSouce;
            }

            int rowItemSize = this.GridListBox.RowItemSize;
            int sourceCount = data.Length;
            for (int i = 0; i < sourceCount; )
            {
                rowItemSize = Math.Min(rowItemSize, sourceCount - i);
                object[] array = new object[rowItemSize];
                for (int j = 0; j < rowItemSize; j++)
                {
                    array[j] = data[i++];
                }
                itemsSouce.Add(array);
            }
        }
    }
}
